import Collector from "./Collector";
import GPSHandler from "../mev/gps/gps_handler";

function TaskManager() {
    this._viewer = window.cesiumViewer;
    this._taskName = '';
    this._trackDataSource = null;
    this._targetDataSource = null;
}
TaskManager.taskTreeData = [
    {
        name:"采集",
        id:'1',
        children: [],
        checked:true
    },
    {
        name:"GNSS轨迹",
        id:'2',
        checked:true
    }
]
//实例
TaskManager.__ginstance = null;
TaskManager.instance = function() {
    if(TaskManager.__ginstance == null){
        TaskManager.__ginstance = new TaskManager();
    }
    return TaskManager.__ginstance;
};

//创建任务
TaskManager.prototype.getTreeData = function (name) {
    return this._taskTreeData;
}

//创建任务
TaskManager.prototype.createTask = function (name) {
    this.clear();
    TaskContext.createTask(name);
    this._taskName = name;
    return true;
}

//打开任务
TaskManager.openTask = function () {

}

TaskManager.listenUpdate=function(fun){
    window.addEventListener('EVENT_UPDATE',fun);
}
TaskManager.unlistenUpdate = function(func){
    window.removeEventListener('EVENT_UPDATE',func);
}

//添加标注
TaskManager.prototype.addMarker = function (marker) {
    let id = this.generateId();
    let pt = new Cesium.Entity({
        name: marker.name,
        id:id,
        position: marker.position,
        billboard:{
            image: Collector.defaultStyle.image,
            horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            scale: 1,
        },
        label: {
            fillColor: Cesium.Color.YELLOW,
            showBackground: true,
            font: "14px monospace",
            horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
            verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
            pixelOffset: new Cesium.Cartesian2(0, -10),
            text:marker.name
        }
    });
    this.getOrCreateTargetDataSource().entities.add(pt);
    TaskManager.taskTreeData[0].children.push({
        id:id,
        name:marker.name,
        data:marker,
        cesiumdata:pt,
        checked:true
    });
    this.update();
}

//删除标注
TaskManager.prototype.deleteMarker = function (marker) {

}

TaskManager.prototype.handleTrackChecked = function(c){
    // if(!this._trackDataSource){
    //     return;
    // }
    // if(c && !this._viewer.dataSources.contains(this._trackDataSource)){
    //     this._viewer.dataSources.add(this._trackDataSource);
    // }
    // if(!c && this._viewer.dataSources.contains(this._trackDataSource)){
    //     this._viewer.dataSources.remove(this._trackDataSource);
    // }
    GPSHandler.getInstance().setShowGPSTrack(c);
}

//删除轨迹
TaskManager.prototype.addGNSSPoint = function (pos) {
    let coords = pos.coords;
    let pt = new Cesium.Entity({
        name: "GNSS.TRACK",
        position: Cesium.Cartesian3.fromDegrees(coords.longitude, coords.latitude),
        point: {
            color: Cesium.Color.YELLOW, //点位颜色
            pixelSize: 6 //像素点大小
        },
        pos:pos
    });
    this.getOrCreateTrackDataSource().entities.add(pt);
}

TaskManager.prototype.getOrCreateTrackDataSource = function () {
    if(this._trackDataSource == null){
        this._trackDataSource = new Cesium.CustomDataSource("Task.Track.DS");
        this._viewer.dataSources.add(this._trackDataSource);
    }
    return this._trackDataSource;
}
TaskManager.prototype.getOrCreateTargetDataSource = function () {
    if(this._targetDataSource == null){
        this._targetDataSource = new Cesium.CustomDataSource("Task.Target.DS");
        this._viewer.dataSources.add(this._targetDataSource);
    }
    return this._targetDataSource;
}


TaskManager.prototype.update = function () {
    const evt = new CustomEvent('EVENT_UPDATE', {
        detail:{
            data:TaskManager.taskTreeData
        }
    })
    window.dispatchEvent(evt);
}

TaskManager.prototype.clear = function () {
    if(this._trackDataSource){
        this._trackDataSource.entities.removeAll();
        this._viewer.dataSources.remove(this._trackDataSource);
        this._trackDataSource = null;
    }

}
TaskManager.prototype.generateId=function() {
    return (
        (Math.random() * 10000000).toString(16).substr(0, 4) +
        "-" +
        new Date().getTime() +
        "-" +
        Math.random()
            .toString()
            .substr(2, 5)
    );
}
export default TaskManager;